package controller

import (
	"Graphite/model"
	"Graphite/service"
	"Graphite/util"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
)

func RoleInsert(c *gin.Context) {

	var role model.Role
	fmt.Println("role",role)
	if err := c.Bind(&role); err != nil {
		util.Return(c, 200,util.PARA_ERROR, err)
		return
	}else {
		util.CreateOne(c, "role", role, "name", role.Name)
		return
	}
}

//直接查找所有角色

func FindAllRole(c *gin.Context)  {
	jsonStr:=util.Retrieve(c,"role","name","")
	//格式转化成结构体
	var role  []model.Role
	_:json.Unmarshal(jsonStr,&role)
	util.Return(c,200,util.SUCCESS,role)
}

//输入姓名模糊查找角色

func FindAllRoleByName(c *gin.Context)  {
	var  key model.Key
	key.Key = c.Query("key")
	jsonStr:=util.Retrieve(c,"role","name",key.Key)
	//格式转化成结构体
	var role  []model.Role
	_:json.Unmarshal(jsonStr,&role)
	util.Return(c,200,util.SUCCESS,role)
}

//根据角色返回对应有无的API

func FindRoleAPI(c *gin.Context)  {
	//var  key model.Key
	//if err := c.Bind(&key); err != nil {
	//	util.Return(c, 200,util.PARA_ERROR, err)
	//	return
	//}
	var role model.Role
	if err := c.Bind(&role); err != nil {
		util.Return(c, 200,util.PARA_ERROR, err)
		return
	}
	service.FindRoleAPI(c,role)
}

func InsertRoleApi(c *gin.Context)  {
	var role model.Role
	if err := c.Bind(&role); err != nil {
		util.Return(c, 200,util.PARA_ERROR, err)
		return
	}
	service.InsertRoleApi(c,role)
}
func DeleteRoleApi(c *gin.Context)  {
	var role model.Role
	if err := c.Bind(&role); err != nil {
		util.Return(c, 200,util.PARA_ERROR, err)
		return
	}
	service.DeleteRoleApi(c,role)
}

func RoleUpdate(c *gin.Context)  {
	var role model.Role
	if err := c.Bind(&role); err != nil {
		util.Return(c, 200,util.PARA_ERROR, err)
		return
	}
	//service.RoleUpdate(c,role)
	util.UpdateOne(c,"role",role,"_id",role.Id)

}



func RoleDelete(c *gin.Context)  {
	var key model.Key
	if err:=c.Bind(&key);err !=nil {
		util.Return(c,  200,util.PARA_ERROR, err)
		return
	}else {
		fmt.Println("key:",key.Id)
		util.DeleteOne(c,"role","_id",key.Id)
	}
}
